package com.hzhjhh.view.cashierView;

import java.awt.Font;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Vector;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import com.hzhjhh.entity.Sale;
import com.hzhjhh.entity.Stock;
import com.hzhjhh.init.dialog.Mytable;
import com.hzhjhh.init.dialog.SettleDialog;
import com.hzhjhh.mapper.SaleMapper;
import com.hzhjhh.mapper.StockMapper;
import com.hzhjhh.util.DBUtils;
import com.hzhjhh.view.cashierView.constant.ShowConstants;
import com.hzhjhh.view.cashierView.enums.MerchandiseShowEnum;

/**
 * 组件初始化-收银页面-实收
 * @author 12083
 *
 */
public class ActualReceivableInit {
	
	private static SaleMapper saleMapper = DBUtils.getSqlSession(true).getMapper(SaleMapper.class);
	private static StockMapper stockPileMapper = DBUtils.getSqlSession(true).getMapper(StockMapper.class);

	/**
	 * 初始化实收-标签
	 */
	public static JLabel initLb() {
		JLabel actualReceivable = new JLabel();
		actualReceivable.setBounds(251, 516, 88, 40);
		actualReceivable.setFont(new Font("宋体", Font.PLAIN, 30));
		actualReceivable.setText("实收:");
		return actualReceivable;
	}
	/**
	 * 初始化实收-输入框
	 */
	public static JTextField initTxf(VenditionDialog venditionDialog) {
		JTextField actualReceivable = new JTextField();
		actualReceivable.setFont(new Font("宋体", Font.PLAIN, 30));
		actualReceivable.setText("0");
		actualReceivable.setBounds(335, 520, 125, 40);
		// 添加失焦事件
		actualReceivable.addFocusListener(new FocusAdapter() {

			@Override
			public void focusLost(FocusEvent e) {
				eventHandle(venditionDialog);
			}
		});
		// 添加按键事件
		actualReceivable.addKeyListener(new KeyAdapter() {

			@Override
			public void keyPressed(KeyEvent e) {
				// enter按键执行的操作
				if (e.getKeyCode() != KeyEvent.VK_ENTER) {
					return;
				}
				eventHandle(venditionDialog);
			}
		});
		return actualReceivable;
	}
	
	private static void eventHandle(VenditionDialog venditionDialog) {
		JTextField txfActualReceivable = venditionDialog.getTxfActualReceivable();
		JLabel lbReceivable = venditionDialog.getLbReceivable2();
		JLabel lbGiveChange = venditionDialog.getLbGiveChaneg2();
		JTextField txfMerchandiseCode = venditionDialog.getTxfMerchandiseCode();
		JTextField txfMerchandiseNum = venditionDialog.getTxfMerchandiseNum();
		JScrollPane jScrollPane = venditionDialog.getJScrollPane1();
		JTextField txfVipCardId = venditionDialog.getTxfVipCardId();
		String actRecStr = txfActualReceivable.getText();
		String recStr = lbReceivable.getText();
		try {
			BigDecimal actRec = new BigDecimal(actRecStr);
			BigDecimal rec = new BigDecimal(recStr);
			if (actRec.compareTo(rec) < 0) {
				JOptionPane.showMessageDialog(txfActualReceivable, "输入的金额不足", "错误", JOptionPane.ERROR_MESSAGE);
				return;
			}
			BigDecimal giveChange = actRec.subtract(rec);

			lbGiveChange.setText(giveChange.toString());
		} catch (Exception ex) {
			JOptionPane.showMessageDialog(txfActualReceivable, "输入的金额有误", "错误", JOptionPane.ERROR_MESSAGE);
			return;
		}
		String cardId = txfVipCardId.getText();
		SettleDialog sett = new SettleDialog(lbReceivable.getText(), txfActualReceivable.getText(), lbGiveChange.getText());
		txfVipCardId.setEditable(true); // 会员卡号可编辑
		txfVipCardId.setText("");
//		// 商品编号 销售日期 商品数量 商品单价 折扣 会员卡号 商品名称
		Vector<Vector<String>> rowDatas = venditionDialog.getRowDatas();
		int num1 = rowDatas.size();
		int numIndex = ShowConstants.MERCHANDISE_HEAD.indexOf(MerchandiseShowEnum.NUM.getCloumnShowName());
		int codeIndex = ShowConstants.MERCHANDISE_HEAD.indexOf(MerchandiseShowEnum.CODE.getCloumnShowName());
		int priceIndex = ShowConstants.MERCHANDISE_HEAD.indexOf(MerchandiseShowEnum.SALE_PRICE.getCloumnShowName());
		int discountIndex = ShowConstants.MERCHANDISE_HEAD.indexOf(MerchandiseShowEnum.DISCOUNT.getCloumnShowName());
		int nameIndex = ShowConstants.MERCHANDISE_HEAD.indexOf(MerchandiseShowEnum.NAME.getCloumnShowName());
		List<Stock> stockPileList = new ArrayList<>();
		List<Sale> saleList = new ArrayList<>();
		String currentTimeStr = venditionDialog.getLbCurrentTime2().getText();
		Date currentTime = new Date();
		for (Vector<String> rowData : rowDatas) {
			int num = Integer.parseInt(rowData.get(numIndex));
			String code = rowData.get(codeIndex);
			
			Stock stockPile = new Stock();
			stockPile.setMerchandiseCode(code);
			stockPile.setNumber(num);
			stockPileList.add(stockPile);
			
			Sale sale = new Sale();
			sale.setSaleProductId(code);
			sale.setSaleId(currentTimeStr);
			sale.setSaleDate(currentTime);
			sale.setSaleProductId(currentTimeStr);
			sale.setSaleNumber(num);
			sale.setSalePrice(new BigDecimal(rowData.get(priceIndex)));
			sale.setSaleDiscount(Integer.parseInt(rowData.get(discountIndex)));
			sale.setUpdatedDate(currentTime);
			sale.setSaleShroffId(cardId);
			sale.setSaleProductName(rowData.get(nameIndex));
			saleList.add(sale);
		}
		// 批量更新库存和销售记录
		stockPileMapper.batchUpdateByNumberAndCode(stockPileList);
		saleMapper.batchInsertByEntity(saleList);
		
//		while (num1 < colnames4.size()) { // 更新数据库
//			Vector ls = new Vector();
//			ls = (Vector) colnames4.get(num1);
//			// ls.get(0).toString();//商品编号
//			// jLabel11.getText();//时间
//			Integer number = new Integer(ls.get(4).toString()); // 数量
//			Double Price = new Double(ls.get(5).toString()); // 单价
//			// ls.get(6).toString();//折扣
//			// jTextField6.getText();//会员卡号
//			// ls.get(1).toString();//商品名称
//			UpdateData.out_StockPile(number, ls.get(0).toString());
//			Insert_stock.Insert_Sale(ls.get(0).toString(), lbCurrentTime2.getText(), number, Price, ls.get(6).toString(),
//					txfVipCardId.getText(), ls.get(1).toString());
//			num1++;
//		}
		txfMerchandiseCode.setText("");
		txfMerchandiseNum.setText("1");
//		colnames4.removeAllElements();
//		jTable1 = Mytable.maketable(colnames4, ShowConstants.MERCHANDISE_HEAD); // 显示内容
		jScrollPane.getViewport().add(
			Mytable.maketable(new Vector<>(), ShowConstants.MERCHANDISE_HEAD)
		); // 把表装入容器
	}
}
